package com.bookstore.controller;

import com.bookstore.dto.ApiResponse;
import com.bookstore.dto.UserDTO;
import com.bookstore.entity.User;
import com.bookstore.service.AdminUserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/users")
public class AdminUserController extends BaseController<User, UserDTO, Long> {
    
    private final AdminUserService adminUserService;
    
    public AdminUserController(AdminUserService adminUserService) {
        super(adminUserService);
        this.adminUserService = adminUserService;
    }
    
    /**
     * 管理员登录
     * @param loginRequest 登录请求参数
     * @return 登录结果
     */
    @PostMapping("/login")
    public ResponseEntity<ApiResponse<UserDTO>> login(@RequestBody UserDTO loginRequest) {
        try {
            UserDTO userDTO = adminUserService.login(loginRequest.getUsername(), loginRequest.getPassword());
            if (userDTO != null) {
                return ResponseEntity.ok(ApiResponse.success("登录成功", userDTO));
            } else {
                return ResponseEntity.badRequest().body(ApiResponse.error("用户名或密码错误"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("登录失败：" + e.getMessage()));
        }
    }
    
    /**
     * 获取当前登录管理员信息
     * @return 当前管理员信息
     */
    @GetMapping("/current")
    public ResponseEntity<ApiResponse<UserDTO>> getCurrentUser() {
        try {
            UserDTO currentUser = adminUserService.getCurrentUser();
            if (currentUser != null) {
                return ResponseEntity.ok(ApiResponse.success("获取成功", currentUser));
            } else {
                return ResponseEntity.badRequest().body(ApiResponse.error("未登录或登录已过期"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取当前用户信息失败：" + e.getMessage()));
        }
    }
    
    /**
     * 管理员退出登录
     * @return 退出结果
     */
    @PostMapping("/logout")
    public ResponseEntity<ApiResponse<Void>> logout() {
        try {
            adminUserService.logout();
            return ResponseEntity.ok(ApiResponse.success("退出登录成功", null));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("退出登录失败：" + e.getMessage()));
        }
    }
    
    /**
     * 重置用户密码
     * @param id 用户ID
     * @return 操作结果
     */
    @PostMapping("/{id}/reset-password")
    public ResponseEntity<ApiResponse<Void>> resetPassword(@PathVariable Long id) {
        try {
            UserDTO userDTO = adminUserService.findById(id);
            userDTO.setPassword("123456"); // 设置默认密码
            adminUserService.update(id, userDTO);
            return ResponseEntity.ok(ApiResponse.success("密码重置成功", null));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("密码重置失败：" + e.getMessage()));
        }
    }
    
    /**
     * 修改管理员密码
     * @param passwordRequest 密码修改请求
     * @return 操作结果
     */
    @PostMapping("/change-password")
    public ResponseEntity<ApiResponse<Void>> changePassword(@RequestBody Map<String, String> passwordRequest) {
        try {
            String oldPassword = passwordRequest.get("oldPassword");
            String newPassword = passwordRequest.get("newPassword");
            
            if (oldPassword == null || newPassword == null) {
                return ResponseEntity.badRequest().body(ApiResponse.error("旧密码和新密码不能为空"));
            }
            
            boolean success = adminUserService.changePassword(oldPassword, newPassword);
            if (success) {
                return ResponseEntity.ok(ApiResponse.success("密码修改成功", null));
            } else {
                return ResponseEntity.badRequest().body(ApiResponse.error("旧密码不正确"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("密码修改失败：" + e.getMessage()));
        }
    }
}